package com.towker.core.mybatis.dialect;

/**
 * @author badqiu
 */
public class SQLServerDialect extends Dialect
{
	public boolean supportsLimitOffset()
	{
		return false;
	}

	public boolean supportsLimit()
	{
		return true;
	}

	static int getAfterSelectInsertPoint(String sql)
	{
		int selectIndex = sql.toLowerCase().indexOf("select");
		final int selectDistinctIndex = sql.toLowerCase().indexOf("select distinct");
		return selectIndex + (selectDistinctIndex == selectIndex ? 15 : 6);
	}

	public String getLimitString(String sql, int offset, int limit)
	{
		return getLimitString(sql, offset, null, limit, null);
	}

	public String getLimitString(String querySelect, int offset, String offsetPlaceholder, int limit, String limitPlaceholder)
	{
		if (offset > 0)
		{
			throw new UnsupportedOperationException("sql server has no offset");
		}
		// if(limitPlaceholder != null) {
		// throw new UnsupportedOperationException(" sql server not support variable limit");
		// }
		return new StringBuffer(querySelect.length() + 8).append(querySelect).insert(getAfterSelectInsertPoint(querySelect), " top " + limit).toString();
	}

	public static void main(String[] args)
	{
		String sql = "select * from users";
		Dialect dia = new SQLServerDialect();
		String str = dia.getLimitString(sql, 0, 10);
		System.out.println(str);
	}
	// TODO add Dialect.supportsVariableLimit() for sqlserver
	// public boolean supportsVariableLimit() {
	// return false;
	// }
}
